import axios from 'axios'

import { Spin } from 'view-design'

const spin: any = Spin;


class HttpRequest {
    baseUrl: String = "";
    constructor(baseUrl: String) {
        this.baseUrl = baseUrl;
    }
    getInsideConfig() {
        const config: Object = {
            baseURL: this.baseUrl,
            headers: {}
        }
        return config
    }
    interceptors(instance: any, otherConfig: any = {}) {
        let isSpin: Boolean = 'isSpin' in otherConfig ? otherConfig.isSpin : true;
        // 请求拦截
        instance.interceptors.request.use((config: Object) => {
            if (isSpin) {
                spin.show()
            }

            return config
        },
            (error: any) => {
                return Promise.reject(error);
            })
        instance.interceptors.response.use((res: any) => {
            const { data, status } = res;
            if (isSpin) {
                spin.hide()
            }
          
            return { data, status }
        }, (error: any) => {
            if (isSpin) {
                spin.hide()
            }
            return Promise.reject(error);
        })
    }
    request(options: any, config?: any) {
        const instance = axios.create();
        options = Object.assign(this.getInsideConfig(), options);
        this.interceptors(instance, config);
        return instance(options)
    }
}
export default HttpRequest